package com.darker.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.darker.reggie.common.CustomException;
import com.darker.reggie.entity.Category;
import com.darker.reggie.entity.Dish;
import com.darker.reggie.entity.Setmeal;
import com.darker.reggie.mapper.CategoryMapper;
import com.darker.reggie.service.CategoryService;
import com.darker.reggie.service.DishService;
import com.darker.reggie.service.SetmealService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Resource
    DishService dishService;
    @Resource
    SetmealService setmealService;

    @Override
    public void remove(long id) {
        //查询当前是否关联了菜品，如果已经关联 抛出一个异常
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, id);
        int count = dishService.count(queryWrapper);
        if (count > 0) {
            //  已经关联 抛出一个异常
            throw  new CustomException("当前分类关联了菜品，不能删除");

        }
        //查询当前上是否关联套餐
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, id);
        setmealService.count(wrapper);
        if (count > 0) {
            //  已经关联 抛出一个异常
            throw  new CustomException("当前分类关联了套餐，不能删除");
        }
        //如果没有关联 则正常删除
        super.removeById(id);
    }
}
